using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Web;
using newtelligence.DasBlog.Runtime;
using newtelligence.DasBlog.Web.Core;

namespace newtelligence.DasBlog.Web.Services
{
    /// <summary>
    /// Summary description for CrosspostReferrerHandler.
    /// </summary>
    public class CrosspostReferrerHandler : IHttpHandler
    {
        private static readonly byte[] aggBugBitmap;

        static CrosspostReferrerHandler()
        {
            Assembly asm = Assembly.GetExecutingAssembly();

            using (Stream stm = asm.GetManifestResourceStream(asm.GetName().Name + ".aggbug.gif"))
            {
                aggBugBitmap = new byte[stm.Length];
                stm.Read(aggBugBitmap, 0, (int) stm.Length);
            }
        }

        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            SiteConfig siteConfig = SiteConfig.GetSiteConfig();

            /*
             * This service is a bit diffent from the other handlers, because 
             * the "bugs" are out in the wild regardless of whether the service is
             * enabled. So the service must yield a valid result, regardless of
             * whetr
             */
            if (siteConfig.EnableCrossposts)
            {
                string sourceId = context.Request.QueryString["id"];
                string referrerUrl = context.Request.UrlReferrer != null ? context.Request.UrlReferrer.ToString() : "";

                try
                {
                    ILoggingDataService logService =
                        LoggingDataServiceFactory.GetService(SiteConfig.GetLogPathFromCurrentContext());
                    IBlogDataService dataService =
                        BlogDataServiceFactory.GetService(SiteConfig.GetContentPathFromCurrentContext(), logService);

                    Entry entry = dataService.GetEntry(sourceId);
                    if (entry != null)
                    {
                        // we'll check whether the entry exists just to avoid trash in the DB
                        logService.AddCrosspostReferrer(
                            new LogDataItem(
                                SiteUtilities.GetPermaLinkUrl(siteConfig, sourceId), referrerUrl,
                                context.Request.UserAgent, context.Request.UserHostName));
                    }
                    else
                    {
                        StackTrace st = new StackTrace();
                        logService.AddEvent(
                            new EventDataItem(EventCodes.Error, "Entry was not found: " + sourceId + " " + st,
                                              ""));
                    }
                }
                catch (Exception exc)
                {
                    // absorb
                    ErrorTrace.Trace(TraceLevel.Error, exc);
                }
            }

            context.Response.OutputStream.Write(aggBugBitmap, 0, aggBugBitmap.Length);
            context.Response.ContentType = "image/gif";
            context.Response.StatusCode = 200;
            context.Response.End();
        }

        #endregion
    }
}